package com.xiaoyu.studyworkspace.controller;

import com.xiaoyu.studyworkspace.common.util.BaseContext;
import com.xiaoyu.studyworkspace.common.util.Result;
import com.xiaoyu.studyworkspace.dto.RecordSizeDTO;
import com.xiaoyu.studyworkspace.pojo.Notice;
import com.xiaoyu.studyworkspace.pojo.RecordV3;
import com.xiaoyu.studyworkspace.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/record")
@CrossOrigin("*")
public class RecordController {

    @Autowired
    private RecordService recordService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 最新使用中
     *
     * @return
     */
    @GetMapping("/v3/list")
    public Result getRecordsV3() {
        //token校验通过，刷新缓存
        String currentUserId = BaseContext.getCurrentId();
        //从当前线程种获取
        stringRedisTemplate.opsForValue().set(currentUserId, currentUserId);
        stringRedisTemplate.expire(currentUserId, 5, TimeUnit.SECONDS);
        RecordSizeDTO recordSizeDTO = new RecordSizeDTO();
        recordSizeDTO.setSize(100);
        List<RecordV3> recordV3s = recordService.getRecordsV3(recordSizeDTO);
        Result<List<RecordV3>> success = Result.success(recordV3s, "成功!");
        return success;
    }


    @GetMapping("/notice")
    public Result getNotice() {
        List<Notice> notice = recordService.getNotice();
        Result<List<Notice>> success = Result.success(notice, "成功!");
        return success;
    }


}
